package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	fmt.Println("starting....")
	taskCh := make(chan int, 100)

	ctx, cancel := context.WithCancel(context.Background())
	for i := 0; i < 100; i++ {
		taskCh <- i
	}
	go worker(ctx, taskCh)
	for {
		select {
		case <-time.After(10 * time.Second):
			cancel()

		case <-time.After(1 * time.Hour):
			return
		}
	}

}

func worker(ctx context.Context, taskCh <-chan int) {
	workCount := 5
	for i := 0; i < workCount; i++ {
		go func(id int) {
			for {
				select {
				case <-ctx.Done():
					fmt.Println(111)
					fmt.Println("worker stop....")
					return
				default:
					task := <-taskCh
					fmt.Println(fmt.Sprintf("current id is:%d, task is:%d", id, task))
					time.Sleep(2 * time.Second)
				}
			}

		}(i)
	}
}
